package com.redteam.xomtro.views;

import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.widget.ScrollView;

public class ObservableScrollView extends ScrollView {
    private ScrollCallbacks mCallbacks;
    private ScrollListener mListener;
    private int maxTop = 0;
    private int screenHeight;
    private float paddingBelowEventTitle;

    public ObservableScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        super.onScrollChanged(l, t, oldl, oldt);
        if (mCallbacks != null) {
            mCallbacks.onScrollChanged(paddingBelowEventTitle, l, t, oldl, oldt);
        }
        if (mListener != null) {
            if (t <= 0)
                mListener.onScrollOnTop(l, t, oldl, oldt);
            if (maxTop == t)
                mListener.onScrollBottom(l, t, oldl, oldt);
            if ((t - oldt) > 0)
                mListener.onScrollUp(paddingBelowEventTitle, l, t, oldl, oldt);
            else if ((t - oldt < 0))
                mListener.onScrollDown(paddingBelowEventTitle, l, t, oldl, oldt);
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        maxTop = getChildAt(0).getHeight() - getHeight();
        paddingBelowEventTitle = screenHeight - (screenHeight/5);
        getChildAt(0).setPadding(0, (int)paddingBelowEventTitle, 0, 0);
    }

    @Override
    public int computeVerticalScrollRange() {
        return super.computeVerticalScrollRange();
    }

    public void setCallbacks(ScrollCallbacks listener) {
        mCallbacks = listener;
    }

    public void setScrollListener(ScrollListener listener) {
        mListener = listener;
    }

    @Override
    public void draw(Canvas canvas) {
        super.draw(canvas);
    }

    public void setScreenHeight(int screenHeight) {
        this.screenHeight = screenHeight;
    }

    public static interface ScrollCallbacks {
        public void onScrollChanged(float paddingBelowEventTitle, int l, int top, int oldl, int oldt);
    }

    public interface ScrollListener {
        public void onScrollOnTop(int l, int t, int oldl, int oldt);

        public void onScrollBottom(int l, int t, int oldl, int oldt);

        public void onScrollUp(float paddingBelowEventTitle, int l, int t, int oldl, int oldt);

        public void onScrollDown(float paddingBelowEventTitle, int l, int t, int oldl, int oldt);
    }
}